/**
 * @File         : home.py
 * @Time         : 2016/9/21 17:24
 * @Author       : Mark
 * @Description  :主页
 */
jQuery.fn.extend({
    uploadPreview: function (opts) {
        var _self = this,
            _this = $(this),
            _data = [],
            _i = 0;
        opts = jQuery.extend({
            Obj: "ImgPr",
            Width: 100,
            Height: 130,
            url: 0,
            data: new FormData(),
            node: 'fill//',
            ImgType: ["gif", "jpeg", "jpg", "bmp", "png"],
            Callback: function () {
            }
        }, opts || {});
        _self.getObjectURL = function (file) {
            var url = null;
            if (window.createObjectURL != undefined) {
                url = window.createObjectURL(file)
            } else if (window.URL != undefined) {
                url = window.URL.createObjectURL(file)
            } else if (window.webkitURL != undefined) {
                url = window.webkitURL.createObjectURL(file)
            }

            opts.url = url;
            _self.node(file);
        };
        _self.node = function (file) {
            opts.node = $("<li ><span class='dge'></span>" +
                " <div><img src='" + opts.url +
                "' class='img-thumbnail img-style'/></div>" +
                " <input type='text'  class='form-control' name='note" + _i +
                "' placeholder='请输入证件名称'></li>");
            $("#" + opts.Obj).append(opts.node);
        };
        _this.change(function () {
            opts.url == 0 ? '' : $("span.btn-success").unbind("click"), $(".thumbnail span.dge").unbind("click");
            if (this.value) {
                if (!RegExp("\.(" + opts.ImgType.join("|") + ")$", "i").test(this.value.toLowerCase())) {
                    alert("选择文件错误,图片类型必须是" + opts.ImgType.join("，") + "中的一种");
                    this.value = "";
                    return false
                }

                if ($.support.msie) {
                    try {
                        $.each(_self[0].files, function (i, file) {
                            _data.append('file' + _i + i, file);

                            _self.getObjectURL(file);
                            _i++;

                        });
                    } catch (e) {
                        _self.getObjectURL(files);
                        $("#" + opts.Obj).append(opts.node);
                        var src = "";
                        var obj = $(".img-style");
                        var div = obj.parent("div")[0];
                        _self.select();
                        if (top != self) {
                            window.parent.document.body.focus()
                        } else {
                            _self.blur()
                        }
                        src = document.selection.createRange().text;
                        document.selection.empty();
                        obj.hide();
                        obj.parent("div").css({
                            'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)',
                            'width': opts.Width + '%',
                            'height': opts.Height + 'px'
                        });
                        div.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = src;
                        _data.append('file' + _i, file);
                        _i++;
                        obj.removeClass("img-style");
                    }
                } else {
                    $.each(_self[0].files, function (i, file) {
                        _self.getObjectURL(file);
                        _data.push(file);
                        _i++;
                    });
                }

                $("span.btn-success").click(function () {
                    opts.Callback(_data);
                });

                $(".thumbnail span.dge").click(function () {
                    var num = $(".thumbnail span.dge").index(this);
                    _data.splice(num, 1);
                    $(this).parents("li").remove();
                })
            }
        });
    }
});